<?php

namespace app\modules\frontend\controllers;

use app\lib\DataMessage;
use app\models\User;
use app\models\UserIdentity;
use app\modules\frontend\LoginFilter;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;

class UserController extends BaseController {
    public function behaviors() {
        return [
            'access' => [
                'class' => LoginFilter::className(),
                'only'=> ['logout', 'index', 'info'],
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => ['logout' => ['post'],],
            ],
        ];
    }

    public function actions() {
        return [
            // 获取验证码页面
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? '213123' : null,
                'offset' => -4,
            ],
        ];
    }

    // 登录页面
    public function actionLogin($message='') {
        Yii::$app->user->logout();

        if (Yii::$app->request->isPost) {
            $userName = Yii::$app->request->post('user_name');
            $password = Yii::$app->request->post('password');
            $model = UserIdentity::findOne(['user_name' => $userName]);
            if ($model && $model->validatePassword($password) && Yii::$app->user->login($model, 10000000)) {
                return DataMessage::initFail(302, '登陆成功', Yii::$app->user->returnUrl);
            }
            return DataMessage::initFail(1, '账号密码错误');
        }

        return $this->render('/user/login', [
            'message' => $message,
        ]);
    }

    // 注册页面
    public function actionRegister($message='') {
        Yii::$app->user->logout();

        if (Yii::$app->request->isPost) {
            $model = new UserIdentity();
            $model->user_name = Yii::$app->request->post('user_name');
            $model->password = Yii::$app->request->post('password');
            if(empty($model->user_name) || empty($model->password)) {
                return DataMessage::initFail(1, '请输入用户名和密码');
            }
            $model->password = sha1($model->password);
            $model->avatar = 'images/avatar.jpg';

            // TODO 在save之前，需要做大量的检查，比如账号是否存在等等
            if (!$model->save()) {
                $errors = $model->errors;
                return DataMessage::initFail(500, reset($errors)[0]);
            }
            Yii::$app->user->login($model);
            return DataMessage::initFail(302, '注册成功', Yii::$app->user->returnUrl);

        }

        return $this->render('/user/register', [
            'message' => $message,
        ]);
    }

    public function actionLogout() {
        Yii::$app->user->logout();
        return $this->goBack();
    }

    public function actionIndex() {
        /** @var User $user */
        $user = Yii::$app->user->identity;

        return $this->render('index', [
            'user' => $user,
        ]);
    }

    public function actionInfo() {
        /** @var User $user */
        $user = Yii::$app->user->identity;

        return $this->render('info', [
            'user' => $user,
        ]);
    }

    public function actionInfoSubmit() {
        /** @var User $user */
        $user = Yii::$app->user->identity;

        $user->load(Yii::$app->request->post(), '');
        if(!$user->save()) {
            return DataMessage::initFail(1, $user->errors);
        }
        return DataMessage::initSuccess();
    }
}
